/*
 * @Description:
 * @Author: your name
 * @version:
 * @Date: 2024-05-08 16:11:33
 * @LastEditors: your name
 * @LastEditTime: 2024-09-10 09:39:09
 */
import { defineStore } from 'pinia'

export const useLineData = defineStore('lineData', {
  state: () => {
    return {
      lineData: [],
      lines: [], // 地铁线路数据
      viewer: null,
      tileset: null,
    }
  },
  getters: {
    Viewer() {
      return this.viewer
    },
    Tile() {
      return this.tileset
    },
    // 获取线路显示隐藏信息
    lineDisplay() {
      return this.lineData.map((item) => ({
        checked: item.checked,
        id: item.id,
        name: item.name,
      }))
    },
    // 获取全部信息
    allData() {
      return this.lineData
    },
    // 获取地铁线路数据
    getLines() {
      return this.lines
    },
  },
  actions: {
    setData(data) {
      return new Promise((resolve, reject) => {
        this.lineData = data.length ? data : []
        resolve(data)
      })
    },
    setViewer(viewer) {
      return new Promise((resolve, reject) => {
        this.viewer = viewer
        resolve(viewer)
      })
    },
    setTileset(tileset) {
      return new Promise((resolve, reject) => {
        this.tileset = tileset
        resolve(tileset)
      })
    },
    // 设置地铁线路数据
    setLines(lines) {
      this.lines = lines
    },
    // 添加单条地铁线路
    addLine(line) {
      this.lines.push(line)
    },
    // 更新线路显示状态
    updateLineVisibility(lineName, isVisible) {
      const lineIndex = this.lines.findIndex((line) => line.name === lineName)
      if (lineIndex !== -1) {
        this.lines[lineIndex].show = isVisible
      }
    },
    // 删除线路
    removeLine(lineName) {
      const lineIndex = this.lines.findIndex((line) => line.name === lineName)
      if (lineIndex !== -1) {
        this.lines.splice(lineIndex, 1)
      }
    },
    // 清空所有线路
    clearLines() {
      this.lines = []
    },
  },
})

export const useMeasureData = defineStore('measureData', {
  state: () => {
    return {
      measureData: [],
    }
  },
  getters: {
    // 获取全部信息
    allData() {
      return this.measureData
    },
  },
  actions: {
    setData(data) {
      return new Promise((resolve, reject) => {
        this.measureData = data.length ? data : []
        resolve(data)
      })
    },
  },
})
